package com.monster.valid.validated;

import com.monster.exception.impl.MonsterNoTackException;
import com.monster.utils.StringUtils;
import com.monster.valid.annotation.ValidIdCard;
import com.monster.valid.common.ValidExceptionEnum;
import com.monster.valid.utils.ValidCommonUtils;
import com.monster.valid.utils.ValidIdCardUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 字符串校验类
 *
 * @author kuangxz
 */
public class IdCardValidator implements ConstraintValidator<ValidIdCard, Object> {
    ValidIdCard valid;

    @Override
    public void initialize(ValidIdCard constraintAnnotation) {
        valid = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (ValidCommonUtils.checkNull(valid.isNull(), valid.name(), valid.message(), value)) {
            return true;
        }
        // 验证身份证位数
        if (!ValidIdCardUtils.check(value.toString())) {
            ValidCommonUtils.exception(ValidExceptionEnum.VALID_ID_CARD, valid.name(), valid.message(), "请输入正确的15位或者18位身份证号码");
        }

        return true;
    }
}
